<template>
  <div class="page">
    <div class="login-container">
      <div class="name">
        <h1>文档管理系统</h1>
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginMsg"
        :rules="rules"
        label-width="auto"
        class="login-form"
        @keyup.enter.prevent="submitForm(loginFormRef)"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginMsg.username"
            type="text"
            autocomplete="off"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginMsg.password"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
            show-password
            :prefix-icon="Lock"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm(loginFormRef)"
            v-loading.fullscreen.lock="fullscreenLoading"
            style="width: 100%"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const fullscreenLoading = ref(false)

const router = useRouter()
const loginFormRef = ref()

const loginMsg = reactive({
  username: '',
  password: '',
})

const rules = reactive({
  username: [{ required: true, message: '请输入用户名', tigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', tigger: 'blur' }],
})

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      fullscreenLoading.value = true
      const res = await axios.get(
        'https://mock.apifox.com/m1/4019805-0-default/login',
        {
          params: {
            username: loginMsg.username,
            password: loginMsg.password,
          },
        }
      )
      fullscreenLoading.value = false
      if (res.data.status === 200) {
        localStorage.setItem('token', res.data.data.token)
        ElMessage({
          message: '登录成功',
          type: 'success',
        })
        router.push('/')
      } else {
        ElMessage({
          message: '登录失败，请检查账号和密码是否正确',
          type: 'error',
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped>
.page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: linear-gradient(to bottom, #f8f9fa, #e9f2fe);
}

.login-container {
  display: flex;
  flex-direction: column;
  width: 400px;
  background-color: #fff;
  border-radius: 10px;
  padding: 30px 0;
  box-shadow: 0px 10px 30px 5px rgba(0, 0, 0, 0.15);
}

.name h1 {
  text-align: center;
  margin: 10px 0;
}

.login-form {
  padding: 0 50px;
}

.login-button {
  display: flex;
  justify-content: end;
}
</style>
